using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.ImageFilter;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class SnsUserMainPage : BasePage
	{
		private const string TAG = "UserMainPage";
		private const int firstFetchCount = 2;
		private const int commonFetchCount = 40;
		public Contact contact;
		public LbsPeople mLbsPeople;
		public ShakePeople shakePeople;
		private SnsInfoTranslater snsInfoTranslater;
		private MMObservableCollection<SnsUserPageItemData, SnsUserPageItem> itemSource = new MMObservableCollection<SnsUserPageItemData, SnsUserPageItem>();
		private List<SnsInfo> snsInfoList;
		private SnsPageSource pageSrc;
		private ScrollViewer scrollViewer;
		private bool bIsFirstLoad = true;
		private int nowIndex;
		public bool bHasMsgboxShown;
		private bool canGoback;
		private EventWatcher mWatchSnsInfo;
		internal Grid LayoutRoot;
		internal MMListBox mListBox;
		private bool _contentLoaded;
		public SnsUserMainPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.UserMainPage_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.UserMainPage_Unloaded));
		}
		private void UserMainPage_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void UserMainPage_Loaded(object sender, RoutedEventArgs e)
		{
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.canGoback = true;
			});
			if (this.bIsFirstLoad)
			{
				this.InitData();
				if (this.contact != null && this.contact.strUsrName == AccountMgr.getCurAccount().strUsrName)
				{
					SnsPostService.resumeUploadAndPost();
				}
				this.InitColorResource();
				BasePage prevPage = PageManager.getInstance().GetPrevPage();
				if (prevPage is CircleMainPage)
				{
					this.OnSetTransition();
				}
				else
				{
					this.OnClearTransition();
				}
				this.bIsFirstLoad = false;
			}
			if (this.scrollViewer == null)
			{
				this.scrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this, typeof(ScrollViewer));
				if (this.scrollViewer != null)
				{
					FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.scrollViewer, 0) as FrameworkElement;
					if (frameworkElement != null)
					{
						VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
						if (visualStateGroup != null)
						{
							visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.group_CurrentStateChanging));
						}
						VisualStateGroup visualStateGroup2 = UIElementTraveler.FindVisualState(frameworkElement, "VerticalCompression");
						if (visualStateGroup2 != null)
						{
							visualStateGroup2.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.vgroup_CurrentStateChanging));
						}
					}
				}
			}
		}
		private void vgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			if (e.get_NewState().get_Name() == "CompressionTop")
			{
				if (this.pageSrc == null)
				{
					return;
				}
				this.pageSrc.refreshFirsPage();
				List<SnsInfo> list = this.pageSrc.getList();
				if (list != null)
				{
					this.addSnsInfos(list);
				}
			}
			if (e.get_NewState().get_Name() == "CompressionBottom")
			{
				if (this.itemSource == null || this.itemSource.get_Count() == 0)
				{
					return;
				}
				if (this.pageSrc == null)
				{
					return;
				}
				this.pageSrc.getRefreshCount();
				this.pageSrc.refreshItem(this.nowIndex);
				List<SnsInfo> list2 = this.pageSrc.getList(this.nowIndex, 40);
				if (list2 != null)
				{
					this.addSnsInfos(list2);
					this.nowIndex += list2.get_Count();
				}
			}
			e.get_NewState().get_Name() == "NoVerticalCompression";
		}
		private void group_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			e.get_NewState().get_Name() == "Scrolling";
		}
		private Contact GetContactFromObj(object object_0)
		{
			if (object_0 is Contact)
			{
				return object_0 as Contact;
			}
			if (object_0 is ShakePeople)
			{
				this.shakePeople = (object_0 as ShakePeople);
				Contact contact = StorageMgr.contact.method_0(this.shakePeople.strUserName);
				if (contact == null)
				{
					contact = new Contact
					{
						strUsrName = this.shakePeople.strUserName,
						strNickName = this.shakePeople.strNickName,
						nSnsBGObjectID = this.shakePeople.nSnsBGObjectID,
						strSnsBGImgID = this.shakePeople.strSnsBGImgID,
						nSnsFlag = this.shakePeople.nSnsFlag,
						nSource = new uint?(26u)
					};
				}
				return contact;
			}
			if (object_0 is LbsPeople)
			{
				this.mLbsPeople = (object_0 as LbsPeople);
				Contact contact2 = StorageMgr.contact.method_0(this.mLbsPeople.strUserName);
				if (contact2 == null)
				{
					contact2 = new Contact
					{
						strUsrName = this.mLbsPeople.strUserName,
						strNickName = this.mLbsPeople.strNickName,
						nSnsBGObjectID = this.mLbsPeople.nSnsBGObjectID,
						strSnsBGImgID = this.mLbsPeople.strSnsBGImgID,
						nSnsFlag = this.mLbsPeople.nSnsFlag,
						nSource = new uint?(18u)
					};
				}
				return contact2;
			}
			return null;
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && Enumerable.Count<object>(object_0) == 1)
			{
				this.registerEvent();
				this.contact = this.GetContactFromObj(object_0[0]);
				if (this.contact != null && this.contact.strUsrName == AccountMgr.strUsrName)
				{
					ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_SNS_CHECK_SELFPAGE, "1");
				}
				this.snsInfoTranslater = new SnsInfoTranslater();
				this.snsInfoList = new List<SnsInfo>();
				this.InitHardCodeData();
			}
		}
		protected override void OnPageOut()
		{
			this.unRegisterEvent();
		}
		protected override void OnPageBack()
		{
			this.bHasMsgboxShown = false;
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (!this.canGoback)
			{
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		private void InitColorResource()
		{
		}
		private void InitHardCodeData()
		{
			if (this.contact == null)
			{
				return;
			}
			SnsUserPageItemData snsUserPageItemData = new SnsUserPageItemData(null);
			snsUserPageItemData.strObjectId = "Cover";
			this.itemSource.Add(snsUserPageItemData);
			if (this.contact.strUsrName == AccountMgr.strUsrName)
			{
				SnsUserPageItemData snsUserPageItemData2 = new SnsUserPageItemData(null);
				snsUserPageItemData2.strObjectId = "TakingPhotoIcon";
				snsUserPageItemData2.bIsNeedShowData = true;
				snsUserPageItemData2.nCreateTime = (uint)Util.getNowSeconds();
				this.itemSource.Add(snsUserPageItemData2);
			}
			SnsUserPageItemData snsUserPageItemData3 = new SnsUserPageItemData(null);
			snsUserPageItemData3.strObjectId = "Loading";
			this.itemSource.Add(snsUserPageItemData3);
			this.itemSource.Bind(this.mListBox);
		}
		private void InitData()
		{
			if (this.contact == null)
			{
				return;
			}
			this.pageSrc = SnsPageMgr.getPageSrc(this.contact.strUsrName);
			if (this.pageSrc == null)
			{
				return;
			}
			this.pageSrc.refreshFirsPage();
			this.snsInfoList = this.pageSrc.getList(0, 40);
			if (this.snsInfoList != null)
			{
				this.addSnsInfos(this.snsInfoList);
				this.nowIndex = this.snsInfoList.get_Count();
			}
			if (this.itemSource != null && this.itemSource.get_Count() != 0)
			{
				SnsUserPageItemData snsUserPageItemData = this.itemSource[this.itemSource.get_Count() - 1];
				if (this.snsInfoList != null && this.snsInfoList.get_Count() > 0)
				{
					snsUserPageItemData.strObjectId = "ToLoad";
				}
				else
				{
					snsUserPageItemData.strObjectId = "Loading";
				}
				this.itemSource[this.itemSource.get_Count() - 1] = snsUserPageItemData;
				return;
			}
		}
		private void registerEvent()
		{
			if (this.mWatchSnsInfo == null)
			{
				this.mWatchSnsInfo = new EventWatcher(this, this, new EventHandlerDelegate(this.OnTimeLineEvent));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_DEL, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_MGR_SNSPAGE_PAGE_ATTACH_ITEM, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_START, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_END, this.mWatchSnsInfo);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsInfo != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_DEL, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_MGR_SNSPAGE_PAGE_ATTACH_ITEM, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_START, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_END, this.mWatchSnsInfo);
				this.mWatchSnsInfo = null;
			}
		}
		private void OnTimeLineEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<SnsInfo> listObject = evtArgs.getListObject<SnsInfo>();
			EventConst mEventID = evtArgs.mEventID;
			switch (mEventID)
			{
			case EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_START:
				if (evtArgs.mObject == null || this.contact == null || (string)evtArgs.mObject != this.contact.strUsrName)
				{
					return;
				}
				if (this.itemSource != null && this.itemSource.get_Count() > 0)
				{
					SnsUserPageItemData snsUserPageItemData = this.itemSource[this.itemSource.get_Count() - 1];
					snsUserPageItemData.strObjectId = "Loading";
					this.itemSource[this.itemSource.get_Count() - 1] = snsUserPageItemData;
					return;
				}
				return;
			case EventConst.ON_MGR_SNSPAGE_REFRESH_PAGE_END:
			{
				if (evtArgs.mObject == null || this.contact == null || (string)evtArgs.mObject != this.contact.strUsrName)
				{
					return;
				}
				List<ulong> snsInfoID = this.getSnsInfoID();
				if (this.pageSrc == null)
				{
					return;
				}
				List<SnsInfo> list = this.pageSrc.syncList(snsInfoID);
				if (list != null)
				{
					this.addSnsInfos(list);
					this.nowIndex += list.get_Count();
				}
				if (this.itemSource != null && this.itemSource.get_Count() > 0)
				{
					SnsUserPageItemData snsUserPageItemData2 = this.itemSource[this.itemSource.get_Count() - 1];
					if (this.pageSrc.isRefreshAllPageDone())
					{
						if (this.snsInfoList != null && this.snsInfoList.get_Count() != 0)
						{
							if (this.pageSrc.getUserType() != SnsUserCatalog.SnsUserFriend && !(this.contact.strUsrName == AccountMgr.curUserName))
							{
								if (this.pageSrc.getUserType() == SnsUserCatalog.SnsUserStranger)
								{
									snsUserPageItemData2.strObjectId = "StrangerNoMorePic";
								}
								else
								{
									if (this.pageSrc.getUserType() == SnsUserCatalog.SnsUserPending)
									{
										snsUserPageItemData2.strObjectId = "NoMorePic";
									}
								}
							}
							else
							{
								snsUserPageItemData2.strObjectId = "NoMorePic";
							}
						}
						else
						{
							if (this.contact.strUsrName == AccountMgr.curUserName)
							{
								snsUserPageItemData2.strObjectId = "TakePicNow";
							}
							else
							{
								if (this.pageSrc.getUserType() == SnsUserCatalog.SnsUserFriend)
								{
									snsUserPageItemData2.strObjectId = "NoMorePic";
								}
								else
								{
									if (this.pageSrc.getUserType() == SnsUserCatalog.SnsUserStranger)
									{
										snsUserPageItemData2.strObjectId = "StrangerNoMorePic";
									}
									else
									{
										if (this.pageSrc.getUserType() == SnsUserCatalog.SnsUserPending)
										{
											snsUserPageItemData2.strObjectId = "NoMorePic";
										}
									}
								}
							}
						}
					}
					else
					{
						if (!this.pageSrc.isPageRefreshing())
						{
							snsUserPageItemData2.strObjectId = "ToLoad";
						}
					}
					this.itemSource[this.itemSource.get_Count() - 1] = snsUserPageItemData2;
					return;
				}
				return;
			}
			case EventConst.ON_MGR_SNSPAGE_PAGE_ATTACH_ITEM:
				break;
			default:
				switch (mEventID)
				{
				case EventConst.ON_STORAGE_SNSINFO_ADD:
					break;
				case EventConst.ON_STORAGE_SNSINFO_DEL:
					this.delSnsInfos(listObject);
					return;
				case EventConst.ON_STORAGE_SNSINFO_MODIFY:
					this.modifySnsInfos(listObject);
					return;
				default:
					return;
				}
				break;
			}
			if (listObject != null)
			{
				this.addSnsInfos(listObject);
				this.nowIndex += listObject.get_Count();
				return;
			}
		}
		public List<ulong> getSnsInfoID()
		{
			if (this.snsInfoList == null)
			{
				return null;
			}
			int num = Enumerable.Count<SnsInfo>(this.snsInfoList);
			return Enumerable.ToList<ulong>(Enumerable.Select<SnsInfo, ulong>(Enumerable.Take<SnsInfo>(this.snsInfoList, num - 1), (SnsInfo snsInfo_0) => snsInfo_0.nObjectID));
		}
		private void addSnsInfos(List<SnsInfo> listInfos)
		{
			if (listInfos != null && listInfos.get_Count() != 0)
			{
				for (int i = listInfos.get_Count() - 1; i >= 0; i--)
				{
					if (this.pageSrc != null && !this.pageSrc.contains(listInfos.get_Item(i)))
					{
						listInfos.RemoveAt(i);
					}
				}
				this.snsInfoTranslater.AddSourceListToDestList(this.itemSource, listInfos);
				this.InsertNewSnsInfo(listInfos);
				if (this.itemSource != null && this.itemSource.get_Count() != 0 && this.itemSource.get_Count() > 1 && this.itemSource[this.itemSource.get_Count() - 1].strObjectId == "TakePicNow")
				{
					SnsUserPageItemData snsUserPageItemData = this.itemSource[this.itemSource.get_Count() - 1];
					snsUserPageItemData.strObjectId = "NoMorePic";
				}
				return;
			}
		}
		private void modifySnsInfos(List<SnsInfo> listInfos)
		{
			if (listInfos != null && listInfos.get_Count() != 0)
			{
				this.snsInfoTranslater.ModifyDestList(this.itemSource, listInfos);
				this.ModifySnsInfo(listInfos);
				return;
			}
		}
		private void delSnsInfos(List<SnsInfo> listInfos)
		{
			if (listInfos != null && listInfos.get_Count() != 0)
			{
				this.snsInfoTranslater.DelSourceListFromDestList(this.itemSource, listInfos);
				this.DelSnsInfo(listInfos);
				return;
			}
		}
		private void InsertNewSnsInfo(List<SnsInfo> listInfos)
		{
			if (this.snsInfoList == null)
			{
				this.snsInfoList = new List<SnsInfo>();
			}
			int i = 0;
			IL_B4:
			while (i < listInfos.get_Count())
			{
				bool flag = false;
				int j = 0;
				while (j < this.snsInfoList.get_Count())
				{
					if (listInfos.get_Item(i).nLocalID != this.snsInfoList.get_Item(j).nLocalID)
					{
						if (listInfos.get_Item(i).strObjectID.CompareTo(this.snsInfoList.get_Item(j).strObjectID) <= 0)
						{
							j++;
							continue;
						}
						flag = true;
						this.snsInfoList.Insert(j, listInfos.get_Item(i));
					}
					IL_8D:
					if (!flag && j == this.snsInfoList.get_Count())
					{
						this.snsInfoList.Add(listInfos.get_Item(i));
					}
					i++;
					goto IL_B4;
				}
				goto IL_8D;
			}
		}
		private void DelSnsInfo(List<SnsInfo> listInfos)
		{
			if (this.snsInfoList != null && this.snsInfoList.get_Count() != 0)
			{
				for (int i = 0; i < listInfos.get_Count(); i++)
				{
					for (int j = 0; j < this.snsInfoList.get_Count(); j++)
					{
						if (listInfos.get_Item(i).nLocalID == this.snsInfoList.get_Item(j).nLocalID)
						{
							this.snsInfoList.RemoveAt(j);
							return;
						}
					}
				}
				return;
			}
		}
		private void ModifySnsInfo(List<SnsInfo> listInfos)
		{
			if (this.snsInfoList != null && this.snsInfoList.get_Count() != 0)
			{
				for (int i = 0; i < listInfos.get_Count(); i++)
				{
					for (int j = 0; j < this.snsInfoList.get_Count(); j++)
					{
						if (listInfos.get_Item(i).nLocalID == this.snsInfoList.get_Item(j).nLocalID)
						{
							this.snsInfoList.set_Item(j, listInfos.get_Item(i));
							return;
						}
					}
				}
				return;
			}
		}
		private void SnsUserPageItem_SnsImageClick(object sender, object object_0)
		{
			SnsUserPageItemData snsUserPageItemData = object_0 as SnsUserPageItemData;
			if (snsUserPageItemData == null)
			{
				return;
			}
			if (snsUserPageItemData.strObjectId == "TakingPhotoIcon")
			{
				MMMessageBox mMMessageBox = MMMessageBox.Show(strings.ChatSendPicDlgTitle, strings.ChatSendPicDlgInfo, true, strings.ChatSendPicDlgTakePhoto, strings.ChatSendPicDlgImgLib);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(this.SelectPicDlgReturn);
				this.bHasMsgboxShown = true;
				return;
			}
			int num = 0;
			if (this.snsInfoList == null)
			{
				return;
			}
			for (int i = 0; i < this.snsInfoList.get_Count(); i++)
			{
				if (snsUserPageItemData.strObjectId == this.snsInfoList.get_Item(i).strObjectID)
				{
					num = i;
					IL_95:
					object[] object_ = new object[]
					{
						this.snsInfoList,
						num,
						this.contact
					};
					base.GoToPage("/Source/Plugin/Sns/Album/UI/AlbumSlideView.xaml", object_, false);
					return;
				}
			}
			goto IL_95;
		}
		public void SelectPicDlgReturn(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				this.TakePhoto();
				return;
			case 1:
				this.ChooserPicture();
				return;
			default:
				this.bHasMsgboxShown = false;
				return;
			}
		}
		private void ChooserPicture()
		{
			MultiPhotoChooser.GoToThisPage(new MultiPhotoChooserConfig
			{
				IsNeedRotate = true,
				CountLimit = 9,
				ForwardPageUri = "/Source/Plugin/Sns/Common/UI/UploadAndPost/SnsPostPage.xaml"
			}, false);
		}
		private void TakePhoto()
		{
			ToolFunction.GotoPageForUserControl("/Source/UI/ImageFilter/ImageFilterPage.xaml", new object[]
			{
				new ImageFilterConfig(null, ImageFilterGetPicMode.GetPicMode_Choose_With_Take, 0, 0, null)
				{
					GetPicMode = ImageFilterGetPicMode.GetPicMode_Take_Photo,
					isSaveToAlbum = ImageFilterMgr.IsSettingAutoSavePhotos(),
					ForwardPageUri = "/Source/Plugin/Sns/Common/UI/UploadAndPost/SnsPostPage.xaml"
				}
			}, false);
		}
		protected override void OnSetTransition()
		{
			TransitionService.SetNavigationOutTransition(this, new NavigationOutTransition
			{
				Backward = new TurnstileTransition
				{
					Mode = TurnstileTransitionMode.BackwardOut
				}
			});
		}
		protected override void OnClearTransition()
		{
			TransitionService.SetNavigationOutTransition(this, null);
		}
		public static void GoToThisPage(object[] passData)
		{
			BasePage topPage = PageManager.getInstance().GetTopPage();
			if (topPage is CircleMainPage)
			{
				CircleMainPage circleMainPage = topPage as CircleMainPage;
				circleMainPage.SetTransition();
			}
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Album/UI/SnsUserMainPage.xaml", passData, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Album/UI/SnsUserMainPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.mListBox = (MMListBox)base.FindName("mListBox");
		}
	}
}
